package com.thinkcube.edutainment.fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Fragment;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.adapters.NotificationAdapter;
import com.thinkcube.edutainment.client.NotificationClient;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.domain.Message;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.service.SPreferences_;

@EFragment(R.layout.notification_page)
public class Notifications extends Fragment {
	@Bean
	OAuthDto oAuthDto;

	@Bean
	NotificationAdapter notificationAdapter;

	@ViewById(R.id.notificationlistView)
	ListView list;

	@RestService
	UserClient userClient;

	@RestService
	NotificationClient notificationClient;

	@ViewById(R.id.progressBar)
	ProgressBar progress;

	@Pref
	SPreferences_ pref;

	@Trace
	// @AfterViews
	@Background
	void gettoken() {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					getNotification(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});
	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	@Bean
	CourseDto courseDto;

	@Background
	void getNotification(OAuth requestToken) {

		courseDto.setAuthToken(requestToken.getoAuth().getToken());

		courseDto.setUserId(pref.getId().get());
		//courseDto.setUserId("5289cf1e6bf841e116dd886d");
		
		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					doMessage(notificationClient.getNotification(courseDto
							.getMultiValuedMap()));
				
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("not connected ");
			}
		});

		
	}

	@UiThread
	void doMessage(Message notifications) {

		if (notifications.getMessageList() != null) {


			//messageAdapter.setTodoData(notifications.getCourseList());
			System.out.println("NOTIFICATION SIZE >>>>"+notifications.getMessageList().size());
			notificationAdapter.setNotificationData(notifications.getMessageList());
			progress.setVisibility(View.GONE);

			// messageAdapter.setTodoData(notifications.getCourseList());
			notificationAdapter.setNotificationData(notifications
					.getMessageList());

			

			list.setAdapter(notificationAdapter);
		}
		
		progress.setVisibility(View.GONE);
	}

	@AfterViews
	void doUi() {
		gettoken();
	}
}
